package Demo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-02-08
 * Time: 16:01
 */
public class Thread_Demo24 {


    public static void main(String[] args) throws InterruptedException {

        Object a = new Object(); // 控制 A
        Object b = new Object(); // 控制 B
        Object c = new Object(); // 控制 C

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (a) {
                    // 等待一次 打印一次A
                    try {
                        a.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                System.out.println("A");

                synchronized (b) {
                    b.notify();
                }
            }
        });

        Thread t2 = new Thread(() -> {

            for (int i = 0; i < 10; i++) {
                synchronized (b) {
                    // b锁等待 A打印完唤醒
                    try {
                        b.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                System.out.println("B");

                synchronized (c) {
                    c.notify();
                }
            }
        });


        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (c) {
                    try {
                        c.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

                System.out.println("C");
                synchronized (a) {
                    a.notify();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(1000); // 确保 t1 先执行

        synchronized (a) {
            a.notify();
        }

         /*Object locker1 = new Object();
         Object locker2 = new Object();
         Object locker3 = new Object();

        Thread t1 = new Thread(() -> {

                for (int i = 0; i < 10; i++) {
                    synchronized (locker1) {
                        try {
                            locker1.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    synchronized (locker2) {
                        locker2.notify();
                    }
                }

        });

        Thread t2 = new Thread(() -> {

                for (int i = 0; i < 10; i++) {
                    synchronized (locker2) {
                        try {
                            locker2.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    synchronized (locker3) {
                        locker3.notify();
                    }
                }

        });

        Thread t3 = new Thread(() -> {

                for (int i = 0; i < 10; i++) {
                    synchronized (locker3) {
                        try {
                            locker3.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    synchronized (locker1) {
                        locker1.notify();
                    }
                }

        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);

        // 从线程 t1 启动
        synchronized (locker1) {
            locker1.notify();
        }*/
    }

}
